# 脚本功能:获取父需求信息
import requests

### variables defination {start}
config_sheetname = "配置"
taskStatusMap = {
    "60418b08c679e20044c09c0c": "其他功能解决",
    "64af9ca33bb6c5ae1239f98e": "待开发评估",
    "654da2f56c338454318df4de": "待发布SIT(组内环境已测)",
    "5e0dabc57d161c00217800b8": "开发中",
    "6604cf0816117c37565adb54": "待开发",
    "5e0db98a8fc7b60022d301c6": "待组内评审",
    "6535cfab96d44880e05a51b1": "待测试",
    "6535cfab96d44880e05a51b4": "测试中",
    "5f40cf2267d6500044ec1a29": "待分析",
    "6535cfab96d44880e05a51b7": "已发布SIT(合入release部署)",
    "5e0dabc57d161c00217800b9": "已发布生产",
    "614407e924ae43003fd05019": "已完成（不发版）",
    "603ddbf166272e0044b3d934": "已发布UAT",
    "5e0dabc57d161c00217800b7": "待产品内审",
    "5e0db97739a39700233b0199": "待审阅",
    "6340edfabb167700408b7339": "经评估无法实现",
    "65e80e978ec65144610baca1": "待架构组评审",
}
### variables defination {end}
### def collection {start}


# fetch task data from Teambition
def fetchSprintData(sprintList):
    data = []
    for sprint in sprintList:
        res = requests.get(
            "https://quality-tools.maycur.com//teambition_script/sprint/task/?name="
            + sprint
        )
        if res.status_code == 200:
            d = res.json()["data"]["results"]
            data.extend(d)
        else:
            print(res.status_code)
    return data


### def collection {end}


########-main-#######
def main():
    config_df = dbt(sheet_name=config_sheetname)
    sprint_list = []
    fatherTasksId = []
    table_data = []
    for v in config_df.itertuples():
        _id, actionName, sprints = v
        if actionName == "自动流程数据同步":
            sprint_list = sprints.strip("[]").split()
    sprint_data = fetchSprintData(sprint_list)
    for d in sprint_data:
        if "parentTaskId" in d and d["parentTaskId"] not in fatherTasksId:
            fatherTasksId.append(d["parentTaskId"])
    for d in fatherTasksId:
        table_data_single = {"taskId": d}
        for sd in sprint_data:
            if sd["id"] == d:
                table_data_single["taskNo"] = "DB-" + str(sd["uniqueId"])
                if taskStatusMap.get(sd["tfsId"], "unknow") not in [
                    "已发布生产",
                    "已完成（不发版）",
                ]:
                    table_data_single["状态"] = "归档"
        table_data.append(table_data_single)
    delete_dbt(sheet_name="父需求列表")
    insert_dbt(table_data, sheet_name="父需求列表")


##### run #####
main()
